<?php

namespace Cck\ArticlesSystem\Http\Requests;

use Illuminate\Validation\Rule;
use Cck\ArticlesSystem\Http\Models\Article;
use Illuminate\Foundation\Http\FormRequest;

class ArticleStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|max:255',
            'category_id' => [
                'integer',
                'required',
                'min:1',
                Rule::exists('categories', 'id')->whereNull('deleted_at')
            ],
            'keyword_id' => [
                'integer',
                'nullable',
                Rule::exists('keywords', 'id')->whereNull('used_at')
            ],
            'catalogue' => [
                'max:50',
                Rule::unique('articles', 'catalogue')->where('slug', $this->input('slug', ''))
            ],
            'slug' => [
                'max:50',
                Rule::unique('articles', 'slug')->where('catalogue', $this->input('catalogue', ''))
            ],
            'cover' => 'string|max:255|url',
            'recommend' => 'boolean',
            'seo_title' => 'max:255',
            'seo_keywords' => 'max:255',
            'seo_description' => 'max:255',
            'views_count' => 'integer|min:0',
            'verify_status' => [
                Rule::in(array_keys(Article::getVerifyStatusOptions()))
            ],
            'published_at' => [
                'date_format:Y-m-d H:i:s'
            ],
            'tags' => 'array',
            'tags.*' => [
                'required',
                'integer',
                'distinct',
                Rule::exists('tags', 'id')
            ],
            'content' => [
                'string',
                'required'
            ]
        ];
    }
}
